// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css'
import './../theme/index.css';
import store from './components/vuex/store'; // 应用vuex
import moment from 'moment'; //时间格式化
import VueClipboard from 'vue-clipboard2'; //复制插件
import Blob from './vendor/Blob';
import Export2Excel from './vendor/Export2Excel';
import GetData from './utils/index'; // 封装的一些常用的方法

import {
  dingTalkPc
} from 'cnf-dingtalk-auth';

import {
  publicFun
} from 'cnf-publicfun';

import {
  ddlwp
} from 'ddlwp';

Vue.use(VueClipboard);
Vue.use(ElementUI);
Vue.prototype.$http = axios;
Vue.config.productionTip = false;

// 时间格式化
Vue.prototype.$moment = function (value, format) {
  if (!value) {
    return '';
  }
  return moment(value).format(format || 'YYYY-MM-DD HH:mm');
};
Vue.prototype.$pageSizes = [10, 20, 30, 40, 50, 100, 200, 500]; //分页大小
//导入过滤器
import './filter';

let jsListApi = ['biz.contact.choose', 'biz.util.uploadImage'];

// 得到url 参数
sessionStorage.setItem(
  'corpId',
  decodeURIComponent(
    (new RegExp('[?|&]' + 'corpId' + '=' + '([^&;]+?)(&|#|;|$)').exec(
      location.href
    ) || [, ''])[1]
    .replace(/\+/g, '%20')
  ) || ''
);
sessionStorage.setItem(
  'appId',
  decodeURIComponent(
    (new RegExp('[?|&]' + 'appId' + '=' + '([^&;]+?)(&|#|;|$)').exec(
      location.href
    ) || [, ''])[1]
    .replace(/\+/g, '%20')
  ) || ''
);

const way = '2'; //1之前 2ddlwp

if (way == '1') {
  dingTalkPc.ddConfig(jsListApi, '/api/isv/auth/getJsConfig').then(
    res => {
      // isvPC签名
      // dingTalkPc.ddConfig(jsListApi, '/api/auth/getJsConfig').then(res => {

      DingTalkPC.runtime.permission.requestAuthCode({
        // 免登
        corpId: sessionStorage.getItem('corpId'),
        onSuccess: function (result) {
          axios
            .get(
              '/api/isv/auth/login' +
              ('?corpId=' +
                sessionStorage.getItem('corpId') +
                '&code=' +
                result.code +
                '&appId=' +
                sessionStorage.getItem('appId'))
            )
            // axios.get(('/api/auth/login') + ("?corpId=" + sessionStorage.getItem("corpId") + "&code=" + result.code + '&appkey=' + sessionStorage.getItem("appkey")))
            .then(
              function (res) {
                if (res.data.success) {
                  sessionStorage.setItem('myPower', res.data.result.qx || '');

                  axios.defaults.headers.common['Authorization'] =
                    res.data.result.token; // 请求头文件
                  new Vue({
                    el: '#app',
                    router,
                    store,
                    components: {
                      App,
                    },
                    template: '<App/>',
                  });
                }
              },
              function (err) {
                ElementUI.Message.warning({
                  message: `免登失败`,
                  type: 'warning',
                });
              }
            );
        },
        onFail: function (err) {
          ElementUI.Message.warning({
            message: `获取临时授权码失败`,
            type: 'warning',
          });
        },
      });
    },
    err => {
      // console.log("111111");
      ElementUI.Message.warning({
        message: err.errorMessage || `签名失败`,
        type: 'warning',
      });
    }
  );
} else {
  // 改成lwp方式不要签名，有此方式免登
  ddlwp(sessionStorage.getItem('corpId')).then(
    res => {
      console.log('ddlwp', res);
      axios.defaults.headers.common['Authorization'] = res.result.token; //请求头文件
      if (res.success) {
        queryRbacNewVue();
      } else {
        ElementUI.Message.warning({
          message: res.errorMsg || '用户信息获取失败',
          type: 'warning',
        });
      }
    },
    err => {
      ElementUI.Message.warning({
        message: err,
        type: 'warning',
      });
    }
  );
  async function queryRbacNewVue() {
    let res = await queryRbac();
    new Vue({
      el: '#app',
      router,
      store,
      components: {
        App,
      },
      template: '<App/>',
    });
  }

  function queryRbac() {
    return new Promise(resolve => {
      axios.get('/api/isv/rbac/queryRbac').then(
        res => {
          if (res.data.success) {
            // axios.defaults.headers.common['Authorization'] = res.data.result.token; //请求头文件
            sessionStorage.setItem('myPower', res.data.result.qx || '');
          }
          resolve('ok');
        },
        err => {
          resolve('ok');
        }
      );
    });
  }
}

// new Vue({
//     el: '#app',
//     router,
//     store,
//     components: {
//         App
//     },
//     template: '<App/>',
// })
